package com.example.controller;

import com.example.domain.Checkid;
import com.example.domain.Resource;
import com.example.service.CheckService;
import com.example.service.CheckidService;
import com.example.service.ResourceService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CheckidController {

    @Autowired
    private CheckidService checkidService;

    @Autowired
    private UserService userService;

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private CheckService checkService;

    @ResponseBody
    @RequestMapping("showAllCheckId")
    public List<Checkid> showAllCheckId(){
        List<Checkid> checkidList = addname();
        return checkidList;
    }

    @ResponseBody
    @RequestMapping("insertCheckId")
    public List<Checkid> insertCheckid(@RequestParam("completetime") String completetime,
                                       @RequestParam("tips") String tips,
                                       HttpServletRequest request){

        //获取当前时间与数据库中的全部时间.
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
        String nowtime = time.format(new Date());

        List<String> timelist = checkidService.showAllCompletetime();
        int length = timelist.size();

        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date deadline=new Date();
        Date now=new Date();
        try {
            now = format1.parse(nowtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //循环数据库里面的所有的盘点单号，一条条判断时间是否超过
        for (int j=0;j<length;j++){
            try {
                deadline = format1.parse(timelist.get(j));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //判断：如果有当前时间小于盘点限制时间，则表示有盘点单是没过期的，不能添加。
            if (now.compareTo(deadline)<0){
                return null;
            }
        }

        //获取当前登录管理员uid
        HttpSession session = request.getSession();
        Integer uid = Integer.parseInt(session.getAttribute("id").toString());

        checkidService.insertCheckid(uid,nowtime,completetime,tips);
        List<Checkid> checkidList = addname();

        return checkidList;
    }

    @ResponseBody
    @RequestMapping("deleteCheckId")
    public List<Checkid> deleteCheckId(@RequestParam("checkid") Integer checkid){
        checkidService.deletCheckId(checkid);
        checkService.deleteCheckByCheckid(checkid);
        List<Checkid> checkidList = addname();
        return checkidList;
    }

    //用于在展示的时候关联工号与名字的一个工具方法
    public List<Checkid> addname(){
        List<Checkid> checkidList = checkidService.showAllCheckid();
        int length=checkidList.size();
        for (int i=0;i<length;i++){
            Checkid checkid = checkidList.get(i);
            if (checkid.getUid()!=0) {
                checkid.setUname(userService.selectUnameById(checkid.getUid()));
                checkidList.set(i,checkid);
            }
        }
        return checkidList;
    }
}
